from .models import *
from operation.models import UserComments, UserPraiseComments
from rest_framework import serializers


class MusicSceneSerializer(serializers.ModelSerializer):
    class Meta:
        model = MusicScene
        fields = ("scene",)



class MusicStyleSerializer(serializers.ModelSerializer):
    class Meta:
        model = MusicStyle
        fields = ("style",)


class MusicEmotionSerializer(serializers.ModelSerializer):
    class Meta:
        model = MusicEmotion
        fields = ("emotion",)


class MusicThemeSerializer(serializers.ModelSerializer):
    class Meta:
        model = MusicTheme
        fields = ("theme",)


class MusicSingerSerializer(serializers.ModelSerializer):
    class Meta:
        model = MusicSinger
        fields = ("singer",)



class MusicComposeSerializer(serializers.ModelSerializer):
    class Meta:
        model = MusicCompose
        fields = ("compose",)


class MusicAlbumSerializer(serializers.ModelSerializer):
    class Meta:
        model = MusicAlbum
        fields = ("album",)


class MusicLyricistSerializer(serializers.ModelSerializer):
    class Meta:
        model = MusicLyricist
        fields = ("lyricist",)


class OneMusicCreateSerializer(serializers.ModelSerializer):
    singer = MusicSingerSerializer(many=True, read_only=True)
    compose = MusicComposeSerializer(many=True, read_only=True)
    lyricist = MusicLyricistSerializer(many=True, read_only=True)
    album = MusicAlbumSerializer(many=True, read_only=True)
    style = MusicStyleSerializer(many=True, read_only=True)
    scene = MusicSceneSerializer(many=True, read_only=True)
    emotion = MusicEmotionSerializer(many=True, read_only=True)
    theme = MusicThemeSerializer(many=True, read_only=True)


    comments_num = serializers.SerializerMethodField(read_only=True)
    comments = serializers.SerializerMethodField(read_only=True)

    def get_comments(self, obj):
        '''
        展示当前歌曲的所有评论以及点赞次数
        :param obj:
        :return:
        '''
        all_comments = UserComments.objects.filter(song=obj)
        comments = {}
        for i in all_comments:
            all_praise = UserPraiseComments.objects.filter(comments=i.pk).count()
            comments[i.user.username] = [i.comments, i.add_time, i.reply_to, all_praise]
        return comments

    def get_comments_num(self, obj):
        '''
        展示当前歌曲的评论数量
        :param obj:
        :return:
        '''
        return UserComments.objects.filter(song=obj).count()


    class Meta:
        model = OneMusicCreate
        fields = "__all__"
